/**
 * MIT License
 *
 * Copyright (c) 2024 小村程序员
 * Email: 397237738@qq.com
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.open.cobertura.core.analyzer;

import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.open.cobertura.core.instrument.CoberturaMethodProbeAdapter;
import org.open.cobertura.core.instrument.CoberturaMethodProbeVisitor;
import org.open.cobertura.core.instrument.IFrame;

/**
 * 方法分析
 */
public class CoberturaMethodAnalyzer extends CoberturaMethodProbeVisitor {

    /**
     * 当前的指令
     */
    private AbstractInsnNode currentInsnNode;
    CoberturaInstructionFactory coberturaInstructionFactory;

    protected CoberturaMethodAnalyzer(CoberturaInstructionFactory coberturaInstructionFactory) {
        super(Opcodes.ASM9);
        this.coberturaInstructionFactory=coberturaInstructionFactory;
    }

    @Override
    public void accept(MethodNode methodNode, MethodVisitor methodVisitor) {
        methodVisitor.visitCode();
        for(AbstractInsnNode node:methodNode.instructions){
            this.currentInsnNode=node;
            node.accept(methodVisitor);
        }
        methodVisitor.visitEnd();
    }
    @Override
    public void visitLabel(Label label) {
        this.coberturaInstructionFactory.addLabel(label);
    }
    /**
     * 访问局部变量指令集
     * @param opcode the opcode of the local variable instruction to be visited. This opcode is either
     *     ILOAD, LLOAD, FLOAD, DLOAD, ALOAD, ISTORE, LSTORE, FSTORE, DSTORE, ASTORE or RET.
     * @param varIndex the operand of the instruction to be visited. This operand is the index of a
     *     local variable.
     */
    @Override
    public void visitInsn(final int opcode) {
        this.coberturaInstructionFactory.addInstruction(currentInsnNode);
    }

    @Override
    public void visitIntInsn(final int opcode, final int operand) {
        this.coberturaInstructionFactory.addInstruction(currentInsnNode);
    }

    @Override
    public void visitVarInsn(final int opcode, final int var) {
        this.coberturaInstructionFactory.addInstruction(currentInsnNode);
    }

    @Override
    public void visitTypeInsn(final int opcode, final String type) {
        this.coberturaInstructionFactory.addInstruction(currentInsnNode);
    }

    @Override
    public void visitFieldInsn(final int opcode, final String owner,
                               final String name, final String desc) {
        this.coberturaInstructionFactory.addInstruction(currentInsnNode);
    }

    @Override
    public void visitMethodInsn(final int opcode, final String owner,
                                final String name, final String desc, final boolean itf) {
        this.coberturaInstructionFactory.addInstruction(currentInsnNode);
    }

    @Override
    public void visitInvokeDynamicInsn(final String name, final String desc,
                                       final Handle bsm, final Object... bsmArgs) {
        //this.coberturaInstructionFactory.addInstruction(currentInsnNode);
    }

    @Override
    public void visitJumpInsn(final int opcode, final Label label) {

        this.coberturaInstructionFactory.addInstruction(currentInsnNode);
        this.coberturaInstructionFactory.addJumpInstruction(label, 1);
    }


    @Override
    public void visitLdcInsn(final Object cst) {
        this.coberturaInstructionFactory.addInstruction(currentInsnNode);
    }

    @Override
    public void visitIincInsn(final int var, final int increment) {
        this.coberturaInstructionFactory.addInstruction(currentInsnNode);
    }

    @Override
    public void visitTableSwitchInsn(final int min, final int max,
                                     final Label dflt, final Label... labels) {
        visitSwitchInsn(dflt, labels);
    }

    @Override
    public void visitLookupSwitchInsn(final Label dflt, final int[] keys,
                                      final Label[] labels) {
        visitSwitchInsn(dflt, labels);
    }

    private void visitSwitchInsn(final Label dflt, final Label[] labels) {
        //this.coberturaInstructionFactory.addInstruction(currentInsnNode);
    }

    @Override
    public void visitMultiANewArrayInsn(final String desc, final int dims) {
        this.coberturaInstructionFactory.addInstruction(currentInsnNode);
    }


    private void visitSwitchInsnWithProbes(final Label dflt,
                                           final Label[] labels) {

    }

    private void visitSwitchTarget(final Label label, final int branch) {

    }

    @Override
    public void insertJumpProbe(int opcode, int index, Label label, IFrame frame) {
        this.coberturaInstructionFactory.addInstruction(currentInsnNode);
        this.coberturaInstructionFactory.addProbe(index,1);

    }

    @Override
    public void insertProbe(int opcode, int index) {
        this.coberturaInstructionFactory.addInstruction(currentInsnNode);
        this.coberturaInstructionFactory.addProbe(index,0);
    }

    @Override
    public void insertProbe(int index) {
        this.coberturaInstructionFactory.addProbe(index,0);
        this.coberturaInstructionFactory.clearCurrentInstruction();
    }



    @Override
    public void visitLineNumber(int line, Label start) {
        this.coberturaInstructionFactory.addCurrentLine(line);
    }
}
